package com.sgb.kjwl.view.ui.activity.self;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.TextView;

import androidx.core.content.ContextCompat;
import androidx.databinding.DataBindingUtil;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.sgb.businessmodule.entity.PickFilterEntity;
import com.sgb.kjwl.AppActivity;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.ActivityJoinCompanyBinding;
import com.sgb.kjwl.di.self.DaggerSelfComponent;
import com.sgb.kjwl.di.self.SelfModule;
import com.sgb.kjwl.model.entity.user.CompanyInfoEntity;
import com.sgb.kjwl.view.ui.adapter.self.JoinComAdapter;
import com.sgb.kjwl.view.widget.BoxSelectorWindow;
import com.sgb.kjwl.viewmodel.self.JoinComModel;
import com.sgb.member.PickCateType;
import com.swgk.core.base.aroute.AppRouteConstant;
import com.swgk.core.base.di.AppComponent;
import com.swgk.core.dialog.BasePopWindow;
import com.swgk.core.recyclerview.adapter.AdapterOnItemClick;
import com.swgk.core.util.MToast;
import com.swgk.core.util.SharedPreferenceUtil;
import com.swgk.core.view.selection.CommonPopEntity;
import com.swgk.core.view.selection.MemberStatusView;
import com.swgk.core.xrecyclerview.XRecyclerView;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

/**
 * 个人中心-加入企业
 */
@Route(path = AppRouteConstant.JoinCompanyActivity)
public class JoinCompanyActivity extends AppActivity {
    public static final int REQUEST_CODE = 101;
    private JoinComAdapter joinCompanyAdapter;
    private ActivityJoinCompanyBinding binding;
    private String text;
    @Inject
    JoinComModel viewModel;

    private String type = "1";  //当前选中搜索类型 企业
    private BoxSelectorWindow typeWindow;

    public static void start(Context context) {
        Intent intent = new Intent();
        intent.setClass(context, JoinCompanyActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected void setupActivityComponent(AppComponent appComponent) {
        DaggerSelfComponent.builder()
                .appComponent(appComponent)
                .selfModule(new SelfModule(this))
                .build()
                .inject(this);
    }

    //设置状态栏为暗
    @Override
    protected boolean statusBarDark() {
        return false;
    }

    @Override
    protected void initView() {
        binding = DataBindingUtil.setContentView(this, R.layout.activity_join_company);
        binding.include.ivBack.setOnClickListener(this);
        binding.include.titleView.setText("员工加入");
        binding.typeLayout.setOnClickListener(this);
        binding.tvSearch.setOnClickListener(this);
        binding.ivDelete.setOnClickListener(this);
        joinCompanyAdapter = new JoinComAdapter(JoinCompanyActivity.this, null);
        binding.recyclerView.setAdapter(joinCompanyAdapter);
        joinCompanyAdapter.setListener(new JoinComAdapter.JoinTvOnClickListener() {
            @Override
            public void onClickJoin(CompanyInfoEntity entity) {
                //企业详情界面
                //ApplyCompanyActivity.start(JoinCompanyActivity.this, entity.getNo());
                //企业身份判定(非企业身份跳转至PC界面验证)
//                if (entity.getType() == 1) {
                    ApplyToJoinTheEnterpriseActivity.start(JoinCompanyActivity.this,entity.getCompName(), entity.getNo(),entity.getType(),entity.getId());
//                } else {
//                    MToast.showToast(JoinCompanyActivity.this, "功能开发中，请前往pc端进行操作");
//                }
            }
        });

        binding.recyclerView.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                viewModel.getCompanyListModel(type, text, false, false);
            }

            @Override
            public void onLoadMore() {
                viewModel.getCompanyListModel(type, text, true, false);
            }
        });

        binding.searchView.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView textView, int actionId, KeyEvent keyEvent) {
                if (actionId == EditorInfo.IME_ACTION_SEARCH || (keyEvent != null && keyEvent.getKeyCode() == KeyEvent.KEYCODE_ENTER)) {
                    closeKeybord();
                    text = binding.searchView.getText().toString();
                    if (TextUtils.isEmpty(text)) {
                        MToast.showToast(JoinCompanyActivity.this, "请输入关键字搜索");
                        return false;
                    }
                    viewModel.getCompanyListModel(type, text, false, false);
                    return true;
                }
                return false;
            }
        });
        binding.searchView.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            }

            @Override
            public void afterTextChanged(Editable editable) {
                text = binding.searchView.getText().toString();
                if (!TextUtils.isEmpty(text)) {
                    viewModel.getCompanyListModel(type, binding.searchView.getText().toString(), false, true);
                } else {
                    showListData(new ArrayList<>(), false);
                }
            }
        });
    }

    @Override
    protected void init() {
        showListData(new ArrayList<>(), false);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.type_layout:
                showChooseIdentityStatus();
                break;
            case R.id.tv_search:
                closeKeybord();
                text = binding.searchView.getText().toString();
                if (TextUtils.isEmpty(text)) {
                    MToast.showToast(JoinCompanyActivity.this, "请输入关键字搜索");
                    return;
                }
                viewModel.getCompanyListModel(type, text, false, false);
                break;
            case R.id.iv_delete:
                binding.searchView.setText("");
                text = binding.searchView.getText().toString();
                showListData(new ArrayList<>(), false);
                break;
        }
    }

    public void showListData(List<CompanyInfoEntity> list, boolean isLoadMore) {
        binding.recyclerView.refreshComplete();
        joinCompanyAdapter.setBlue(text);
        if (TextUtils.isEmpty(text)) {
            joinCompanyAdapter.setDatas(null);
            binding.recyclerView.setVisibility(View.GONE);
            binding.emptyView.setVisibility(View.VISIBLE);
            return;
        }
        if (isLoadMore) {
            if (null != list && list.size() > 0) {
                binding.recyclerView.loadMoreComplete();
                joinCompanyAdapter.addDatas(list);
            } else {
                binding.recyclerView.setNoMore(true);
            }
            binding.recyclerView.setVisibility(View.VISIBLE);
        } else {
            binding.recyclerView.refreshComplete();
            if (null != list && list.size() > 0) {
                binding.recyclerView.setVisibility(View.VISIBLE);
                binding.emptyView.setVisibility(View.GONE);
                joinCompanyAdapter.setDatas(list);
                binding.recyclerView.setLoadingMoreEnabled(true);
            } else {
                joinCompanyAdapter.setDatas(null);
                binding.recyclerView.setVisibility(View.GONE);
                binding.emptyView.setVisibility(View.VISIBLE);
            }
        }
    }

    private void showChooseIdentityStatus() {
        if (typeWindow == null) {
            typeWindow = new BoxSelectorWindow(this);
            typeWindow.setBgView(binding.grayLayout);
            typeWindow.setListener(new BoxSelectorWindow.BoxOnClickListener() {
                @Override
                public void onItemClick(PickFilterEntity entity) {
                    type = entity.getNo();
                    binding.tvType.setText(entity.getName());
                    if (!TextUtils.isEmpty(text)) {
                        viewModel.getCompanyListModel(type, binding.searchView.getText().toString(), false, true);
                    } else {
                        showListData(new ArrayList<>(), false);
                    }
                }
            });
            typeWindow.setOnDisMissClick(new BasePopWindow.OnDisMiss() {
                @Override
                public void disMiss() {
                    binding.ivArrow.setImageDrawable(ContextCompat.getDrawable(JoinCompanyActivity.this, R.mipmap.ic_down_arrow_n));
                    binding.rootPopLayout.setBackgroundColor(Color.TRANSPARENT);
                    binding.typeLayout.setBackground(getResources().getDrawable(R.drawable.shape_search_spinner_frame));
                    binding.bottomView.setVisibility(View.GONE);
                }
            });
            List<PickFilterEntity> list = new ArrayList<>();
            list.add(new PickFilterEntity("1", "企业"));
            list.add(new PickFilterEntity("2", "团队"));
            list.add(new PickFilterEntity("3", "政府"));
            typeWindow.setData(list, 0);
        }
        binding.ivArrow.setImageDrawable(ContextCompat.getDrawable(JoinCompanyActivity.this, R.mipmap.ic_up_arrow_n));
        binding.typeLayout.setBackgroundColor(Color.TRANSPARENT);
        binding.rootPopLayout.setBackground(getResources().getDrawable(R.drawable.member_pop_blue_bg2));
        binding.bottomView.setVisibility(View.VISIBLE);
        typeWindow.showPopwindow0(binding.topLayout);
    }
}
